/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2007 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using G=Gtk;
using Gtk;
using Glade;

using Anculus.Core;
using Anculus.Gui;

using Galaxium.Core;
using Galaxium.Client;
using Galaxium.Protocol;
using Galaxium.Protocol.Gui;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;

namespace Galaxium.Protocol.GaduGadu.GtkGui
{
	public class GaduAccountWidget : BasicAccountWidget
	{
		//protected bool _register = false;
		
		public GaduAccountWidget () : base (GaduProtocol.Instance)
		{
			
		}
		
		public GaduAccountWidget (IProtocol protocol) : base (protocol)
		{
			
		}
		
		public override void Initialize ()
		{
			_presenceCombo = CreateStatusCombo (GaduPresence.Online, //the selected one
					GaduPresence.Online, GaduPresence.Chat, GaduPresence.Away, GaduPresence.Busy,
					GaduPresence.Dnd, GaduPresence.Invisible);
			
			base.Initialize ();
			
			_accountCombo.TooltipText = "Provide the gadu ID number you wish to use. Example: 1234567890";
		}
		
		protected override IAccount SetAccount ()
		{
			GaduAccount account = GetAccount(_accountCombo.Entry.Text) as GaduAccount;
			
			if (account != null)
			{
				account.Password = _passwordEntry.Text;
				account.AutoConnect = _autoconnectCheck.Active;
				account.RememberPassword = _rememberPassCheck.Active;
				account.InitialPresence = GetInitialPresence ();
				
				//account.Register = _register;
				
				return account;
			}
			else
			{
				GaduAccount newAccount = new GaduAccount (_accountCombo.Entry.Text, _passwordEntry.Text, _autoconnectCheck.Active, _rememberPassCheck.Active);
				newAccount.InitialPresence = GetInitialPresence ();
				//newAccount.Register = _register;
				
				AccountUtility.AddAccount (newAccount);
				
				return newAccount;
			}
		}
		
		protected override void LoadAccountInfo ()
		{
			base.LoadAccountInfo ();
			
			// Here we would load all the Gadu specific settings into the UI.
			
			GaduAccount account = Account as GaduAccount;
			
			if (account != null)
			{
				//_register = false;
			}
		}
		
		public override void EnableFields ()
		{
			base.EnableFields ();
			
			// Also enable custom protocol widgets.
		}
		
		public override void DisableFields (bool omit_cancel)
		{
			base.DisableFields (omit_cancel);
			
			// Also disable custom protocol widgets.
		}
		
		protected override Gdk.Pixbuf PresenceImageLookup (IPresence item)
		{
			return IconUtility.GetIcon (GaduPresence.GetIconString (item), IconSizes.Small);
		}
		
		protected override void ConnectButtonClicked (object sender, EventArgs args)
		{
			// When connect is clicked, we create a session and then we send it off to the basic handler
			
			_current_session = CreateSession () as GaduSession;
			
			if (_current_session == null)
				return;
			
			base.ConnectButtonClicked (sender, args);
			
			// After we make the attempt to log in, we want to remove the flag for registration.
			//_register = false;
		}
		
		protected override void SettingsButtonClicked (object sender, EventArgs args)
		{
			//ResponseType response = ResponseType.None;
			//AccountSettingsDialog dialog = new AccountSettingsDialog ();
			
			//dialog.Register = _register;
			
			/*while(response == ResponseType.None)
			{
				response = (ResponseType)dialog.Run();
				
				if (response == ResponseType.Ok)
				{
					//_register = dialog.Register;
					
					dialog.Destroy();
					break;
				}
				else
				{
					dialog.Destroy();
					break;
				}
			}*/
		}
	}
}